package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Movie;
import cn.edu.scnu.entity.PlaybackHistory;
import cn.edu.scnu.service.MovieService;
import cn.edu.scnu.service.PlaybackHistoryService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller

public class HistoryController {

    @Autowired
    private PlaybackHistoryService playbackHistoryService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MovieService movieService;

    @RequestMapping("/history/movieList")
    @ResponseBody
    public Map<String, Object> findUserHistoryMovie(HttpSession httpSession) {
        String phone = (String) httpSession.getAttribute("phone"); // 获取用户手机号
//        int userId = user.getUserId();
        Map<String, Object> response = new HashMap<>();

        List<PlaybackHistory> PlaybackHistoryList = playbackHistoryService.showPlaybackHistory(phone);

        List<Movie> movieList = new ArrayList<>();

        for (PlaybackHistory userPlaybackHistory : PlaybackHistoryList) {
            Movie movie = movieService.findMovieById(userPlaybackHistory.getMovieId());
            movieList.add(movie);
        }

        response.put("movieList", movieList);
        response.put("HistoryList", PlaybackHistoryList);

        return response;
    }

    @RequestMapping("/history")
    public String history(HttpSession httpSession, Model model) {
        String phone = (String) httpSession.getAttribute("phone"); // 获取用户手机号
        if (phone != null) {
            model.addAttribute("phone", phone);
        }
        return "history";
    }
}
